package com.aglhz.nature.presenter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import android.content.Context;
import android.util.Log;

import com.aglhz.nature.constants.ServerAPI;
import com.aglhz.nature.events.BrandIntrouctionEvent;
import com.aglhz.nature.events.ChangeDataViewEvent;
import com.aglhz.nature.events.SetViewEvent;
import com.aglhz.nature.modle.BrandImage;
import com.aglhz.nature.modle.ChangePasswordBean;
import com.aglhz.nature.presenter.base.BasePresenter;
import com.aglhz.nature.utils.AsyncHttpCilentUtil;
import com.aglhz.nature.view.iv.ChangePasswordView;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

public class ChangePasswordPresenter extends BasePresenter{
	private String old_password;
	private String new_password;
	private String ok_password;
	
	private static String TAG = ChangePasswordPresenter.class.getName();
	
	private ChangePasswordView changePasswordView ;
	private ChangePasswordBean changePasswordBean;
	public ChangePasswordPresenter(ChangePasswordView view, Context context) {
		super();
		changePasswordView = view;
		super.setContext(context);
	}
	
	public void loadChangePasswordBean(){
		old_password=changePasswordView.setOld_password();
		new_password=changePasswordView.setNew_password();
		 ok_password=changePasswordView.setOk_password();
		 
		 String url=ServerAPI.APIHEARD+ServerAPI.CHANGE_PASSWORD;
		 RequestParams params = new RequestParams();  
		    params.put("oldPassword", old_password);
		    params.put("password1", new_password);
		    params.put("password2", ok_password);
		 AsyncHttpClient client = AsyncHttpCilentUtil.getInstance(super.getContext());
			client.post(url, params,new TextHttpResponseHandler() {

				@Override
				public void onSuccess(int statusCode,
						cz.msebera.android.httpclient.Header[] headers,
						String responseString) {
					try {
						Log.e(TAG, "response" + responseString);

						Gson gson = new Gson();

						ChangePasswordBean cpBean = gson
								.fromJson(responseString,
										ChangePasswordBean.class);
						changePasswordBean = (ChangePasswordBean) cpBean
								.clone();

						Log.d(TAG, "brandImage---mmm-----"
								+ changePasswordBean);
						Log.d(TAG, "brandImage---111-----"
								+ changePasswordBean.getOther());
						Log.d(TAG, "brandImage---eeeee---"
								+ changePasswordBean.getData());
						int code = Integer.parseInt(changePasswordBean.getOther().getCode());
						if(code == 200){
							// send event
							ChangeDataViewEvent event = new ChangeDataViewEvent(
									ChangeDataViewEvent.EVENT_UPDATE_SUCESS_SHOWTOAST);
							EventBus.getDefault().post(event);

						
						}else{
							// send event
							ChangeDataViewEvent event = new ChangeDataViewEvent(
									ChangeDataViewEvent.EVENT_UPDATE_FAILURE_SHOWTOAST);
							EventBus.getDefault().post(event);
							
						}
						
						
						Log.e(TAG, "success----------------------------"
								+ "success");
					} catch (Exception e) {
						e.printStackTrace();
					}

				}

				@Override
				public void onFailure(int statusCode,
						cz.msebera.android.httpclient.Header[] headers,
						String responseString, Throwable throwable) {

					Log.e(TAG, "failure");
				}

			});

		}
	// /*********************view operation*******************///
			@Subscribe(threadMode = ThreadMode.MAIN)
			public void onEventChangeDataViewThread(ChangeDataViewEvent event) {
				Log.e("------ChangeDataViewEvent---------------","qqqqqqqqqqqqqqqqqq");
				String msg = event.getMessage();
				Log.i(TAG, "onEventSetViewThread:" + msg);
				// update UI By event
				if (ChangeDataViewEvent.EVENT_UPDATE_SUCESS_SHOWTOAST == msg) {
					// update  view
					changePasswordView.showSucessToast();
					Log.e("------EVENT_LOGOUT_VIEW---------------","vvvvvvvvvvvvvvvvvvv");
			
				}if(ChangeDataViewEvent.EVENT_UPDATE_FAILURE_SHOWTOAST == msg){
					this.setFailureMessage();
					changePasswordView.showFailureToast();
				}

			}

	public String  setFailureMessage() {
		return changePasswordBean.getOther().getMessage();
		// TODO Auto-generated method stub
		
	}
	
}
